﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Itech_Run.Domain.Shared.Base;

namespace Itech_Run.Sys.Domain.Entity
{

    public partial class Sys_Module : CustomAggregateRoot<string>
    {

        /// <summary>
        /// Back
        /// </summary>

        [Required]
        [StringLength(50)]
        public string ApplicationCode { get; set; }

        [Required]
        [StringLength(100)]
        public string Name { get; set; }

        public int Level { get; set; }

        [StringLength(255)]
        public string? Url { get; set; }

        public bool? IsShowUI { get; set; }

        [StringLength(100)]
        public string? IconName { get; set; }

        public int Sort { get; set; }

        public bool IsActive { get; set; }

        [StringLength(100)]
        public string? AssemblyName { get; set; }

        [StringLength(100)]
        public string? ApiController { get; set; }

        public virtual ICollection<Sys_ModuleButton> Sys_ModuleButton { get; set; } = new HashSet<Sys_ModuleButton>();

        public virtual ICollection<Sys_RoleModule> Sys_RoleModule { get; } = new HashSet<Sys_RoleModule>();

        public virtual ICollection<Sys_RoleModuleButton> Sys_RoleModuleButton { get; } = new HashSet<Sys_RoleModuleButton>();

        [ForeignKey("Sys_Module")]
        [StringLength(36)]
        public string? ParentId { get; set; }

        public virtual Sys_Module Parent { get; set; }

        public virtual ICollection<Sys_Module> Childs { get; } = new HashSet<Sys_Module>();

        #region action
        public void AllocateModuleButton(List<string> buttonList)
        {
            Sys_ModuleButton = Sys_ModuleButton.Where(p => buttonList.Contains(p.ButtonId)).ToList();
            buttonList = buttonList.Where(p => !Sys_ModuleButton.Any(ur => ur.ButtonId == p)).ToList();

            buttonList.ForEach(item =>
            Sys_ModuleButton.Add(new Sys_ModuleButton
            {
                ModuleId = this.Id,
                ButtonId = item,
                CreatedBy = this.LastModifiedBy,
                CreatedTime = this.LastModifiedTime,
                LastModifiedBy = this.LastModifiedBy,
                LastModifiedTime = this.LastModifiedTime
            }));
        }
        #endregion
    }
}
